<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="dialogVisible = true"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList" v-loading="loading">
          <el-table-column label="序号" align="center" type="index" width="120" fixed="left" />
          <el-table-column label="角色名称" prop="name" align="center" width="240" />
          <el-table-column label="描述" prop="description" align="center" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{ row }">
              <el-button size="small" @click="showAssignPermission(row.id)">分配权限</el-button>
              <el-button size="small" @click="showDialog(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="remove(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination 
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="total"
            layout="prev,pager,next" 
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 添加/编辑弹窗 -->
    <el-dialog :title="roleForm.id ? '编辑角色' : '新增角色'" :visible="dialogVisible" @close="dialogClose">
      <!-- 表单内容 -->
      <el-form ref="roleRef" :model="roleForm" :rules="roleRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹窗 -->
    <AssignPermission
      :visible="assignPermissionVisible"
      @dialog-close="closeAssignPermission"
      :roleId="roleId"
    />
  </div>
</template>

<script>
import { getRoleList, addRole, getRoleDetail, updateRole, deleteRole } from '@/api/setting'
import AssignPermission from './components/assign-permission'
import settingMixins from '@/mixins/setting'

export default {
  components: {
    AssignPermission
  },
  mixins: [settingMixins],
  data() {
    return {
      abc: 'def',
      // 分页参数
      params: {
        page: 1,
        pagesize: 2
      },
      // 角色列表数据
      roleList: [],
      // 总条数
      total: 0,
      // 控制弹窗显示与隐藏
      dialogVisible: false,
      // 弹窗的表单数据对象
      roleForm: {
        name: '',
        description: ''
      },
      // 弹窗的表单验证规则对象
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      // // 角色 id
      // roleId: ''
      assignPermissionVisible: false,
      roleId: '',
      loading: false
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    // 获取角色列表
    async fetchRoleList() {
      this.loading = true
      try {
        const res = await getRoleList(this.params)
        // console.log(res);
        this.roleList = res.data.rows
        this.total = res.data.total
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    // 当页码值发生变化时，触发的回调函数
    handleCurrentChange(page) {
      // 请求参数的页码值赋值为最新的分页器的页码值
      this.params.page = page
      // 刷新列表
      this.fetchRoleList()
    },
    // 弹窗关闭的回调
    dialogClose() {
      this.dialogVisible = false
      this.$refs.roleRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 新增角色
    submit() {
      // 兜底校验
      this.$refs.roleRef.validate(async valid => {
        if (!valid) return
        let res
        // 校验通过
        if (this.roleForm.id) {
          // 发起编辑角色请求
          res = await updateRole(this.roleForm)
        } else {
          // 发起新增角色请求
          res = await addRole(this.roleForm)
        }
        // 提示用户
        this.$message.success(res.message)
        // 关闭弹窗
        this.dialogClose()
        // 刷新表格数据
        this.fetchRoleList()
      })
    },
    // 打开弹窗
    async showDialog(id) {
      // this.roleId = id
      this.dialogVisible = true
      // 获取角色详情
      const res = await getRoleDetail(id)
      console.log(res);
      // 弹窗表单数据回显
      this.roleForm = res.data
    },
    // 删除角色
    remove(id) {
      // 询问用户
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起删除请求
        const res = await deleteRole(id)
        // 提示用户
        this.$message.success(res.message)
        // 如果当前页只有一条数据的时候，让页数减一
        if (this.roleList.length === 1 && this.params.page > 1) {
          this.params.page --
        }
        // 刷新列表
        this.fetchRoleList()
      }).catch(() => {
      });
    },
    // 显示分配权限弹窗
    showAssignPermission(id) {
      this.roleId = id
      this.assignPermissionVisible = true
    },
    // 关闭分配权限弹窗
    closeAssignPermission() {
      this.assignPermissionVisible = false
    }
  }
}
</script>

<style>

</style>
